<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-row>
        <el-col :span="18">
          <el-form-item prop="fuzzy">
            <el-input
              v-model="searchForm.fuzzy"
              style="width: 250px"
              placeholder="仪器名称/仪器编号/仪器型号/仪器品牌"
            />
          </el-form-item>
          <el-form-item prop="type" label="仪器类别">
            <el-select
              v-model="searchForm.type"
              style="width: 145px"
              placeholder="请选择仪器类别"
              clearable
            >
              <el-option
                v-for="item in typleList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="classType" label="检测类别">
            <el-select
              v-model="searchForm.classType"
              style="width: 145px"
              placeholder="请选择检测类别"
              clearable
            >
              <el-option
                v-for="item in classTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              class="iconfont icon-chaxun1"
              size="small"
              @click="handleSearch"
            >
              查询</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div>
      <el-table
        stripe
        border
        :data="factorData"
        class="table-item"
        highlight-current-row
        :header-cell-style="{ background: '#ECF3FC' }"
        style="width: 100%"
      >
        <template slot="empty">
          <div class="table_empty">
            <div class="empty_tips">
              <span> 暂无数据 </span>
            </div>
          </div>
        </template>
        <el-table-column type="index" width="70" align="center" label="序号" />
        <el-table-column
          prop="number"
          label="仪器编号"
          width="180"
          align="center"
        />
        <el-table-column
          prop="name"
          label="仪器名称"
          width="150"
          align="center"
        />
        <el-table-column
          prop="model"
          label="仪器型号"
          width="130"
          align="center"
        />
        <el-table-column
          prop="position"
          label="放置位置"
          width="120"
          align="center"
        />
        <el-table-column
          prop="manufacturer"
          label="仪器品牌"
          width="310"
          align="center"
        />
        <el-table-column
          prop="type"
          label="仪器类别"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.type == '1'">现场采样仪器</span>
            <span v-if="scope.row.type == '2'">现场检测仪器</span>
            <span v-if="scope.row.type == '3'">实验室仪器</span>
            <span v-if="scope.row.type == '4'">校准仪器</span>
            <span v-if="scope.row.type == '5'">样品制备仪器</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="lastCheck"
          label="上次期间核查日期"
          width="140"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="checkCycle"
          label="核查周期"
          width="140"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.checkCycle == '1'">一个月</span>
            <span v-if="scope.row.checkCycle == '2'">三个月</span>
            <span v-if="scope.row.checkCycle == '3'">半年</span>
            <span v-if="scope.row.checkCycle == '4'">一年</span>
            <span v-if="scope.row.checkCycle == '5'">俩年</span>
            <span v-if="scope.row.checkCycle == '6'">三年</span>
            <span v-if="scope.row.checkCycle == '7'">无需检定</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="nextCheck"
          label="下次期间核查日期"
          width="190"
          align="center"
        >
        </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="130">
          <template slot-scope="scope">
            <el-button type="text" @click="View(scope.row)"> 查看</el-button>
            <div class="erc-divider-vertical" />
            <el-button type="text" @click="Instrument(scope.row)"
              >期间核查</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      title="期间核查信息"
      :visible.sync="transferDialogVisible"
      width="50%"
      center
      :close-on-click-modal="false"
    >
      <el-form
        ref="installForm"
        :rules="rules"
        :model="installForm"
        label-width="150px"
      >
        <el-row :gutter="20">
          <el-col :span="11">
            <el-form-item prop="checkCharge" label="核查负责人:">
              <!-- <el-input
                :disabled="isLook"
                style="width: 90%"
                v-model="installForm.checkCharge"
              /> -->
              <el-select
                v-model="accompanyId"
                placeholder="请选择"
                filterable
                value-key="userId"
                style="width: 90%"
                @change="handleCheckedCitiesUser"
              >
                <el-option
                  v-for="item in usernameOptions"
                  :key="item.userId"
                  :label="item.empName"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="checkUser" label="核查人:">
              <!-- <el-input
                :disabled="isLook"
                v-model="installForm.checkUser"
                style="width: 90%"
              /> -->
              <el-select
                v-model="custodianId"
                placeholder="请选择"
                filterable
                value-key="userId"
                @change="handleChecked"
                style="width:90%"
              >
                <el-option
                  v-for="item in usernameOptions"
                  :key="item.userId"
                  :label="item.empName"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item label="上次期间核查日期:" prop="lastDate">{{
              installForm.lastDate
            }}</el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item prop="checkDate" label="本次期间核查日期:">
              <el-date-picker
                :disabled="isLook"
                style="width: 90%"
                v-model="installForm.checkDate"
                value-format="yyyy-MM-dd"
                type="date"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item prop="checkCycle" label="核查周期:">
              <el-select
                v-model="installForm.checkCycle"
                style="width: 90%"
                placeholder="请选择周期"
                clearable
              >
                <el-option
                  v-for="item in timeTypleList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item prop="checkType" label="核查方式:">
              <!-- <el-input
                :disabled="isLook"
                v-model="installForm.checkType"
                style="width: 90%"
              /> -->
              <el-select
                v-model="installForm.checkType"
                style="width: 90%"
                placeholder="请选择周期"
                clearable
              >
                <el-option
                  v-for="item in checkTypeName"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="checkName" label="仪器设备/物质名称:">
              <el-input
                :disabled="isLook"
                v-model="installForm.checkName"
                style="width: 90%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="checkRecord" label="核查记录:">
              <el-input
                :disabled="isLook"
                v-model="installForm.checkRecord"
                style="width: 90%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="demand" label="结果评定要求:">
              <el-input
                :disabled="isLook"
                v-model="installForm.demand"
                style="width: 90%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="result" label="核查结论:">
              <el-radio-group v-model="installForm.result">
                <el-radio :label="1">合格</el-radio>
                <el-radio :label="2">不合格</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="transferDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="Confirm">确认</el-button>
      </div>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog
      title="查看"
      :visible.sync="MyInstrumentView"
      center
      top="5vh"
      width="1400px"
    >
      <div>
        <div class="install-title">期间核查详情信息</div>
        <el-form label-width="130px">
          <el-row :gutter="12">
            <el-col :span="8">
              <el-form-item prop="number" label="仪器编号:">
                {{ ViewForm.number }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="name" label="仪器名称:">
                {{ ViewForm.name }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="model" label="仪器型号:">
                {{ ViewForm.model }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="position" label="放置位置:">
                {{ ViewForm.position }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="manufacturer" label="仪器品牌:">
                {{ ViewForm.manufacturer }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="type" label="仪器类别:">
                <el-form-item>
                  <span v-if="ViewForm.type == '1'">现场采样仪器</span>
                  <span v-if="ViewForm.type == '2'">现场检测仪器</span>
                  <span v-if="ViewForm.type == '3'">实验室仪器</span>
                  <span v-if="ViewForm.type == '4'">校准仪器</span>
                  <span v-if="ViewForm.type == '5'">样品制备仪器</span>
                </el-form-item>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="上次期间核查日期:" prop="lastCheck">
                {{ ViewForm.lastCheck }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="checkCycle" label="核查周期:">
                <el-form-item>
                  <span v-if="ViewForm.type == '1'">一个月</span>
                  <span v-if="ViewForm.type == '2'">三个月</span>
                  <span v-if="ViewForm.type == '3'">半年</span>
                  <span v-if="ViewForm.type == '4'">一年</span>
                  <span v-if="ViewForm.type == '5'">两年</span>
                  <span v-if="ViewForm.type == '6'">三年</span>
                  <span v-if="ViewForm.type == '7'">无需检定</span>
                </el-form-item>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="nextCheck," label="下次期间核查日期:">
                {{ ViewForm.nextCheck }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-dialog>

    <div style="float: right">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"
      />
    </div>
  </div>
</template>

<script>
import * as List from "@/api/List";
import * as api from "@/api/Checklist";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
export default {
  components: { Pagination },
  filters: {},
  data() {
    return {
      limit: 5,
      total: 0,
      isLook: false,
      factorData: [], //数据
      ViewForm: [],
      transferDialogVisible: false,
      MyInstrumentView: false,
      usernameOptions: [],
      accompanyId: [],
      custodianId: [],
      rules: {
        checkDate: [
          {
            required: true,
            message: "本次期间核查日期不能为空",
            trigger: "blur"
          }
        ]
      },
      typleList: [
        {
          name: "现场采样仪器",
          id: 1
        },
        {
          name: "现场检测仪器",
          id: 2
        },
        {
          name: "实验室仪器",
          id: 3
        },
        {
          name: "校准仪器",
          id: 4
        }
      ],
      checkTypeName: [
        {
          name: "传递测量法",
          id: 1
        },
        {
          name: "设备比对法",
          id: 2
        },
        {
          name: "标准物质法",
          id: 3
        },
        {
          name: "留样复测法",
          id: 4
        },
        {
          name: "方法比对法",
          id: 5
        }
      ],
      timeTypleList: [
        {
          name: "一个月",
          id: 1
        },
        {
          name: "三个月",
          id: 2
        },
        {
          name: "半年",
          id: 3
        },
        {
          name: "一年",
          id: 4
        },
        {
          name: "两年",
          id: 5
        },
        {
          name: "三年",
          id: 6
        },
        {
          name: "无需检定",
          id: 7
        }
      ],
      classTypeList: [
        {
          name: "水",
          id: 1
        },
        {
          name: "气",
          id: 2
        },
        {
          name: "噪声",
          id: 3
        },
        {
          name: "土壤、底泥、固废、污泥和生活垃圾",
          id: 4
        },
        {
          name: "样品制备",
          id: 5
        },
        {
          name: "小型分析仪器",
          id: 6
        },
        {
          name: "色谱仪",
          id: 7
        },
        {
          name: "光谱仪",
          id: 8
        },
        {
          name: "质谱仪",
          id: 9
        }
      ],
      installForm: {
        result: 1,
        checkCharge: "",
        checkUser: "",
        checkCycle: ""
      },
      searchForm: {
        pageNumber: 1,
        pageSize: 10
      }
    };
  },
  created() {
    this.handleSearch();
  },
  methods: {
    handleSearch() {
      //   console.log(123);
      api.periodList(this.searchForm).then(res => {
        // console.log(res);
        if (res.code === 200) {
          this.factorData = res.result.records;
          // console.log(this.factorData);
          this.total = res.result.total;
          // console.log(this.factorData);
        }
      });
    },
    //选择处理人
    handleCheckedCitiesUser(val) {
      this.installForm.checkCharge = val.empName;
    },

    //选择负责人
    handleChecked(val) {
      this.installForm.checkUser = val.empName;
    },
    Instrument(row) {
      console.log(row);
      //人
      List.userList().then(res => {
        this.usernameOptions = res.result;
      });
      this.installForm.lastDate = row.lastCheck;
      this.installForm.checkCycle = row.checkCycle;
      console.log(this.installForm.checkCycle);

      this.installForm.deviceId = Number(row.id);
      this.installForm.nextData = row.nextCheck;
      this.transferDialogVisible = true;
    },
    Confirm() {
      this.$refs.installForm.validate(valid => {
        if (valid) {
          this.installForm.result = Number(this.installForm.result);
          api
            .addPeriod(this.installForm)
            .then(request => {
              this.$notify({
                title: "成功",
                message: request.message,
                type: "success"
              });
              this.handleSearch();
              this.transferDialogVisible = false;
              this.$refs.installForm.resetFields();
            })
            .catch(() => {});
        } else {
          return false;
        }
      });
    },
    resultNumber(e) {
      console.log(e);
    },
    // 查看
    View(row) {
      console.log(row);
      this.MyInstrumentView = true;
      this.ViewForm = row;
    }
  }
};
</script>

<style scoped lang="scss">
.clearfix {
  line-height: 40px;
  display: flex;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.clearfix {
  line-height: 40px;
  display: flex;
}
.install-title {
  display: flex;
  align-items: center;
  line-height: 40px;
  padding-left: 20px;
  color: rgb(15, 14, 14);
  background-color: #d7dae2;
  justify-content: flex-start;
  margin-top: 15px;
}
.delete {
  width: 10px;
  height: 10px;
  position: relative;
  top: -10px;
  cursor: pointer;
}
::v-deep .el-upload-list {
  width: 100px;
  display: flex;
  margin: 10px 10px;
}
::v-deep .el-upload-list > li {
  margin: 10px 10px;
}
::v-deep.el-upload-list__item {
  margin: 10px 10px;
}
::v-deep.el-upload-list--picture .el-upload-list__item {
  margin: 10px 10px !important;
}
.user-info-head {
  position: relative;
  display: inline-block;
  height: 120px;
}

::v-deep.avatar-upload-preview {
  width: 400px;
  border-radius: 0%;
  height: 400px;
  > img {
    transform: translateX(0);
  }
}
.user-info-head:hover:after {
  content: "+";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 24px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  line-height: 110px;
  border-radius: 50%;
}
</style>
